let express = require('express')
let http = require('http')
let socketIO = require('socket.io')
let Point = require('./models/Point')
let PathPlan = require('./PathPlan')


async function main() {
    // 创建路径规划对象并读取地图数据
    let pathPlan = new PathPlan()
    await pathPlan.init('./data/map.csv', './data/node.csv')

    // http服务
    let app = express()
    let httpServer = http.Server(app)
    // 设置静态文件目录
    app.use(express.static('./public'));
    // websocket服务
    let io = socketIO(httpServer)
    // 接受websocket路径规划请求
    io.on('connection', function (socket) {
        socket.on('path_planning', function (msg) {
            let {startPoint, endPoint} = msg
            let result = pathPlan.planning(new Point(startPoint.lng, startPoint.lat), new Point(endPoint.lng, endPoint.lat))
            io.emit('path_planning', result);
        });
    })
    // 启动服务
    httpServer.listen(3000);
}

main()